package lockDemo.synchronizeDemo;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
* @author jie
* @version 创建时间：2017年12月6日 下午8:45:59
*  除了使用synchronized外，还可以使用Lock对象来创建临界区。
*/
public class Demo1 {
	private Lock lock = new ReentrantLock();  
	public static void main(String[] args) {
		 final Demo1 rs = new Demo1();  
		   
	       new Thread() {  
	           public void run() {  
	              rs.f();  
	           }  
	       }.start();  
	   
	       new Thread() {  
	           public void run() {  
	              rs.g();  
	           }  
	       }.start();  
	   
	       rs.h();  
	}

    public void f() {  
       // other operations should not be locked...  
       System.out.println(Thread.currentThread().getName()  
              + ":not synchronized in f()");  
       lock.lock();  
       try {  
           for (int i = 0; i < 5; i++) {  
              System.out.println(Thread.currentThread().getName()  
                     + ":synchronized in f()");  
              try {  
                  TimeUnit.SECONDS.sleep(3);  
              } catch (InterruptedException e) {  
                  e.printStackTrace();  
              }  
           }  
       } finally {  
           lock.unlock();  
       }  
    }  
   
    public void g() {  
       // other operations should not be locked...  
       System.out.println(Thread.currentThread().getName()  
              + ":not synchronized in g()");  
       lock.lock();  
       try {  
           for (int i = 0; i < 5; i++) {  
              System.out.println(Thread.currentThread().getName()  
                     + ":synchronized in g()");  
              try {  
                  TimeUnit.SECONDS.sleep(3);  
              } catch (InterruptedException e) {  
                  e.printStackTrace();  
              }  
           }  
       } finally {  
           lock.unlock();  
       }  
    }  
   
    public void h() {  
       // other operations should not be locked...  
       System.out.println(Thread.currentThread().getName()  
              + ":not synchronized in h()");  
       lock.lock();  
       try {  
           for (int i = 0; i < 5; i++) {  
              System.out.println(Thread.currentThread().getName()  
                     + ":synchronized in h()");  
              try {  
                  TimeUnit.SECONDS.sleep(3);  
              } catch (InterruptedException e) {  
                  e.printStackTrace();  
              }  
           }  
       } finally {  
           lock.unlock();  
       }  
    }  
   
}
